<?php
/** 
 * User: Kendy
 * Date: 2019/10/31
 * Time: 14:12
 */
namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;
class HotelRoom extends Model
{
	   /**
	    * [getRoomList 根据酒店id获取酒店房间列表]
	    * @Author   kendy
	    * @DateTime 2019-10-31T11:09:23+0800
	    * @param    [type]                   $hotel_id [description]
	    * @return   [type]                             [description]
	    */
        public function getRoomListById($hotel_id)
        {
            //is_window,is_wc,is_wifi,is_stop_car,is_qian_fuwu,is_food,
            $roomList = Db::name('hotel_room')
            ->field('room_id,bed_scale,square,room_price,room_discount_price,room_images,room_name,is_tehui,is_quxiao,room_other,room_fw_other,food_other')
            ->where(['is_del'=>0,'is_display'=>0,'hotel_id'=>$hotel_id])
            ->select(); 
            if(count($roomList)>0)
            { 
            	foreach ($roomList as $key => $value) {    
                 $room_other = [];
                 $room_fw_other = [];
                 $food_other = [];
                 $roomList[$key]['room_images'] = [];
            		 $rimgarr = Db::name('room_img')  
                      ->field('id,img_path')
		              ->where('room_id',$value['room_id'])
		              ->order('sort desc')
		              ->select();  
                      if(count($rimgarr)>0){
                           foreach ($rimgarr as $k => $v) {
                              if($v['img_path'])
                              $roomList[$key]['room_images'][] = addWaterByField($v['img_path']);
                           }
                      }  
                      if(!empty($roomList[$key]['room_other'])){
                         $where['id'] = ['IN',explode(',', $value['room_other'])];
                         $where1['id'] = ['IN',explode(',', $value['room_fw_other'])];
                         $where2['id'] = ['IN',explode(',', $value['food_other'])];

                           $fw = $this->getDatasName($where); 
                           $fw1 = $this->getDatasName($where1); 
                           $fw2 = $this->getDatasName($where2); 
                          if(count($fw)>0){
                              foreach ($fw as $keys => $value) {
                               $room_other[] = $value['data_name'];
                            }
                            $roomList[$key]['room_other'] =  $room_other; 
                          }else{
                           $roomList[$key]['room_other'] = ['无'];
                          }
                          if(count($fw1)>0){
                              foreach ($fw1 as $keys => $value) {
                               $room_fw_other[] = $value['data_name'];
                            }
                             $roomList[$key]['room_fw_other'] =  $room_fw_other;  
                          }else{
                           $roomList[$key]['room_fw_other'] = ['无'];
                          }
                          if(count($fw2)>0){
                              foreach ($fw2 as $keys => $value) {
                               $food_other[] = $value['data_name'];
                            }
                           $roomList[$key]['food_other'] =  $food_other;  
                          }else{ 
                            $roomList[$key]['food_other'] = ['无'];

                          }

                      }  
            	}
              return $roomList;
            } 
            return [];
        }


        public function getDatasName($where)
        {
          return  Db::name('datas')->field('data_name')->where($where)->select();
        }
}